import { Controller } from "../../mvc/core/Controller";
import { ICountryPositionController } from "./interfaces/ICountryPositionController";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SceneConfig } from "../resource/SceneConfig";
import { GameController } from "../mvc/GameController";
import { ShortcutsKey } from "../../common/shortcutsKey/ShortcutsKey";
import { ModuleType } from "../view/common/ModuleType";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { SServiceCombat } from "../../../Message/Public/SServiceCombat";
import { IView } from "../../mvc/interfaces/IView";
import { CountryPositionModule } from "../../../modules/CountryPositionModule";
import { ISocialModule } from "../../../modules/interfaces/ISocialModule";
import { SocialModule } from "../../../modules/SocialModule";
import { BaseWindow } from "../../component/window/BaseWindow";
import { CountryPositionOperWindow } from "../view/countryPosition/CountryPositionOperWindow";
import { CountryCallTogetherIcon } from "../view/countryPosition/CountryCallTogetherIcon";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { CountryConfig } from "../resource/CountryConfig";
import { ESex } from "../../../Message/Public/ESex";
import { Alert } from "../../../com/mui/controls/Alert";
import { SCountryCallTogether } from "../../../Message/Game/SCountryCallTogether";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { PlayerMiniInfo } from "../model/PlayerMiniInfo";
import { SPlayerGuildInfo } from "../../../Message/Game/SPlayerGuildInfo";
import { SPlayerCountryInfo } from "../../../Message/Game/SPlayerCountryInfo";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { MsgManager } from "../manager/MsgManager";
import { SCountryBuffInfo } from "../../../Message/Game/SCountryBuffInfo";
import { ICountryPositionModule } from "../../../modules/interfaces/ICountryPositionModule";
type int = number;
//class CountryPositionController
    
    export  class CountryPositionController extends Controller implements ICountryPositionController
    {
       

        /* internal  */onCountryStartHandler(arg1: MessageBlock): void
        {
            cache.country.isOpenCountry = true;
            SceneConfig.instance.setNpcIsAddStage(3000104, true);
            SceneConfig.instance.setNpcIsAddStage(3000105, true);
            SceneConfig.instance.setNpcIsAddStage(5000095, true);
            GameController.gameui.getGameui().resetGuild_Society(true);
            ShortcutsKey.instance.addKeyMap(ModuleType.Guild, Language.getString(80417));
            return;
        }

        /* internal  */onOfficerInfoUpdateHandler(arg1: MessageBlock): void
        {
            GameProxy.country.getOfficerInfo();
            return;
        }

        /* internal  */onServiceCombatHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SServiceCombat;
            cache.country.serviceCombat = loc1.combat;
            if (this._countryPosition && cache.role.curWorldLevel >= 85) 
            {
                this._countryPosition.updateServiceCombat();
            }
            return;
        }

        /* internal  */updateFriendListHandler(arg1: Object): void
        {
            if (this._positionOperWindow) 
            {
                this.positionOperWindow.updateFriendList();
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._countryPosition == null) 
            {
                this._countryPosition = new CountryPositionModule();
            }
            return this._countryPosition;
        }

        public get socialModule(): ISocialModule
        {
            if (this._socialModule == null) 
            {
                this._socialModule = new SocialModule();
                this._socialModule.country = (view as BaseWindow).contentContainer;
            }
            return this._socialModule;
        }

        public get positionOperWindow(): CountryPositionOperWindow
        {
            if (this._positionOperWindow == null) 
            {
                this._positionOperWindow = new CountryPositionOperWindow();
            }
            return this._positionOperWindow;
        }

        public get countryCallIcon(): CountryCallTogetherIcon
        {
            if (this._countryCallIcon == null) 
            {
                this._countryCallIcon = new CountryCallTogetherIcon();
                this._countryCallIcon.addEventListener(EventName.CountryCallTogetherIcon, this.onCountryCallIconHandler);
            }
            return this._countryCallIcon;
        }

        /* internal  */onCountryCallIconHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc1=cache.country.sCountryCallTogether;
            if (loc1) 
            {
                loc2 = "";
                if (loc3 = CountryConfig.instance.getCountryPrivilegeInfo(loc1.position)) 
                {
                    if (cache.country.kingSex != ESex._ESexMan) 
                    {
                        loc2 = loc3.nameEx;
                    }
                    else 
                    {
                        loc2 = loc3.name;
                    }
                }
                loc4 = loc2 + loc1.miniPlayer.name + Language.getString(36430);
                Alert.okLabel = Language.getString(36431);
                Alert.calcelLabel = Language.getString(36432);
                Alert.buttonWidth = 80;
                Alert.show(loc4, null, Alert.OK | Alert.CANCEL, null, this.onCountryCallCloseHandler);
            }
            return;
        }

        /* internal  */onCountryCallCloseHandler(arg1: int): void
        {
            var index: int;
            var info: SCountryCallTogether;

            var loc1;
            info = null;
            index = arg1;
            if (index == Alert.OK) 
            {
                info = cache.country.sCountryCallTogether;
                with ({}) 
                {
                };
                info.fromPos.map.convey(info.fromPos.x, info.fromPos.y, 
#error('pushwith') {}
                , 
#error('pushwith') {}
                .success = (arg1): void
                {
                    deleteCountryCallInfo();
                    return;
                }, info.fromPos.proxyId, info.fromPos.serverId)
            }
            return;
        }

        /* internal  */deleteCountryCallInfo(): void
        {
            this.countryCallIcon.hideInChatView();
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.CountryPositionOpen, this.onCountryPositionOpenHandler);
            Dispatcher.addEventListener(EventName.CountryGetOfficerInfo, this.onCountryGetOfficerInfoHandler);
            Dispatcher.addEventListener(EventName.CountryGetOfficerInfoSuccess, this.onGetOfficerInfoSuccessHandler);
            Dispatcher.addEventListener(EventName.CountryOperPosition, this.onCountryOperPositionHandler);
            Dispatcher.addEventListener(EventName.CountryOperPositionSuccess, this.onCountryOperPositionSuccessHandler);
            Dispatcher.addEventListener(EventName.CountryCallTogether, this.onCountryCallTogetherHandler);
            Dispatcher.addEventListener(EventName.CountryOpenCountryBuff, this.onOpenCountryBuffHandler);
            Dispatcher.addEventListener(EventName.CountryForbidTalking, this.onCountryForbidTalkingHandler);
            Dispatcher.addEventListener(EventName.GuildDisbandSuccess, this.onGuildDisbandSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildCreateSuccess, this.onGuildCreateSuccessHandler);
            Dispatcher.addEventListener(EventName.OpenGuildWindow, this.onOpenGuildWindowHandler);
            Dispatcher.addEventListener(EventName.CountryAppointFriendPosition, this.onAppointFriendPositionHandler);
            Dispatcher.addEventListener(EventName.GuildQuitSuccess, this.onGuildDisbandSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildUpdateInfo, this.onGuildUpdateInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryUpdateInfo, this.onCountryUpdateInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryCallTogether, this.onCountryCallTogetherMsgHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryCallTogetherSuccess, this.onCountryCallTogetherSuccessHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryBuff, this.onCountryBuffHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryStart, this.onCountryStartHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicOfficerInfoUpdate, this.onOfficerInfoUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicServiceCombat, this.onServiceCombatHandler);
            NetDispatcher.addCmdListener(ServerCommand.FriendListUpdate, this.updateFriendListHandler);
            NetDispatcher.addCmdListener(ServerCommand.FriendAdd, this.updateFriendListHandler);
            NetDispatcher.addCmdListener(ServerCommand.FriendRemove, this.updateFriendListHandler);
            return;
        }

        /* internal  */onCountryPositionOpenHandler(arg1: DataEvent): void
        {
            if (this._socialModule && !this._socialModule.isHide) 
            {
                this.socialModule.hide();
            }
            else if (cache.country.isOpenCountry) 
            {
                this.socialModule.show();
            }
            return;
        }

        /* internal  */onCountryGetOfficerInfoHandler(arg1: DataEvent): void
        {
            GameProxy.country.getOfficerInfo();
            return;
        }

        /* internal  */onGetOfficerInfoSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            cache.country.officerInfos = loc1;
            if (this._countryPosition) 
            {
                this._countryPosition.updateOfficerInfo(loc1);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryPositionUpdate));
            return;
        }

        /* internal  */onCountryOperPositionHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.country.operPosition(loc1.fromPlayerId, loc1.toPlayerId, loc1.operType, loc1.targetPosition);
            return;
        }

        /* internal  */onCountryOperPositionSuccessHandler(arg1: DataEvent): void
        {
            GameProxy.country.getOfficerInfo();
            if (this._positionOperWindow && !this._positionOperWindow.isHide) 
            {
                this._positionOperWindow.hide();
            }
            return;
        }

        /* internal  */onCountryCallTogetherHandler(arg1: DataEvent): void
        {
            GameProxy.country.callTogether();
            return;
        }

        /* internal  */onOpenCountryBuffHandler(arg1: DataEvent): void
        {
            GameProxy.country.openCountryBuff();
            return;
        }

        /* internal  */onCountryForbidTalkingHandler(arg1: DataEvent): void
        {
            var e: DataEvent;
            var playerMiniInfo: PlayerMiniInfo;
            var tips: string;
            var onCloseHandler: Function;

            var loc1;
            playerMiniInfo = null;
            onCloseHandler = null;
            e = arg1;
            onCloseHandler = (arg1: int): void
            {
                var loc1=null;
                if (arg1 == Alert.OK) 
                {
                    loc1 = playerMiniInfo.entityId;
                    GameProxy.country.forbidTalking(loc1);
                }
                return;
            }
            playerMiniInfo = e.data as PlayerMiniInfo;
            if (playerMiniInfo == null) 
            {
                return;
            }
            tips = Language.getStringByParam(36433, playerMiniInfo.playerName);
            Alert.show(tips, null, Alert.OK | Alert.CANCEL, null, onCloseHandler);
            return;
        }

        /* internal  */onGuildDisbandSuccessHandler(arg1: DataEvent): void
        {
            if (this._socialModule && !this._socialModule.isHide) 
            {
                this._socialModule.updateGuildPanel(false);
            }
            return;
        }

        /* internal  */onGuildCreateSuccessHandler(arg1: DataEvent): void
        {
            this._isAfterCreateSuccess = true;
            return;
        }

        /* internal  */onGuildUpdateInfoHandler(arg1: SPlayerGuildInfo): void
        {
            var loc1=false;
            if (arg1 && !(arg1.guildId == 0)) 
            {
                loc1 = true;
            }
            if (this._socialModule && !this._socialModule.isHide) 
            {
                this._socialModule.updateGuildPanel(loc1);
            }
            return;
        }

        /* internal  */onOpenGuildWindowHandler(arg1: DataEvent): void
        {
            if (cache.country.isOpenCountry) 
            {
                this.socialModule.show();
                this.socialModule.setTabSelected(0);
            }
            return;
        }

        /* internal  */onAppointFriendPositionHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            this.positionOperWindow.show();
            this.positionOperWindow.position = loc1.position;
            this.positionOperWindow.posName = loc1.posName;
            return;
        }

        /* internal  */onCountryUpdateInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPlayerCountryInfo;
            cache.country.selfCountryInfo = loc1;
            GameProxy.country.getOfficerInfo();
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountrySelPositionGot));
            return;
        }

        /* internal  */onCountryCallTogetherMsgHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCountryCallTogether;
            cache.country.sCountryCallTogether = loc1;
            this.countryCallIcon.showInChatView();
            if (this._callSecTimer == null) 
            {
                this._callSecTimer = new SecTimer();
                this._callSecTimer.addListener(TimerType.ENTERFRAME, this.onTimerEnterFrameHandler);
                this._callSecTimer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
            }
            this._callSecTimer.reset();
            this._callSecTimer.repeatCount = 15;
            this._callSecTimer.start();
            return;
        }

        /* internal  */onTimerEnterFrameHandler(arg1: SecTimer): void
        {
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: SecTimer): void
        {
            arg1.stop();
            arg1.isDelete = false;
            this.countryCallIcon.hideInChatView();
            return;
        }

        /* internal  */onCountryCallTogetherSuccessHandler(arg1: MessageBlock): void
        {
            MsgManager.showRollTipsMsg(Language.getString(36434));
            return;
        }

        /* internal  */onCountryBuffHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCountryBuffInfo;
            return;
        }

        private /* var */_countryPosition: ICountryPositionModule;

        private /* var */_countryCallIcon: CountryCallTogetherIcon;

        private /* var */_socialModule: ISocialModule;

        private /* var */_positionOperWindow: CountryPositionOperWindow;

        private /* var */_callSecTimer: SecTimer;

        private /* var */_isAfterCreateSuccess: Boolean=false;
    }
